const { Controller } = require('egg');

class HomeController extends Controller {
  async shareadd() {
    const { app, ctx } = this;

    ctx.validate({
      file_id: {
        type: "string",
        required: true,
        defValue: "",
        desc: "file_id",
      },
    })
    let currentUser = ctx.authUser;

    let f = await app.model.File.findOne({
      where: {
        id: ctx.request.body.file_id,
        user_id: currentUser.id
      }

    })
    console.log(f, "f")
    if (!f) {
      return ctx.throw(400, "该文件不存在")
    }
    let shareurl = ctx.genID(15);
    const res = await app.model.Share.create({
      file_id: ctx.request.body.file_id,
      user_id: currentUser.id,
      sharedurl: shareurl,
      isCancel: 0
    })

    //192.168.2.32自己的主机端口
    let url = 'http://192.168.2.32:7001/share/' + shareurl
    ctx.apiSuccess(url, "创建成功", 200);


  }
  async sharelist() {
    let currentUser = this.ctx.authUser;
    const res = await this.app.model.Share.findAll({
      where: {
        user_id: currentUser.id
      },
      include: [{
        model: this.app.model.File
      }]
    })

    console.log(res, "ress")
    this.ctx.apiSuccess(res,"获取成功",200)
    
  }
  async ckfenxiang() {
    this.ctx.validate({
      file_id: {
        type: "string",
        required: true,
        defValue: "",
        desc: "file_id",
      },
      sharedurl: {
        type: "string",
        required: true,
        defValue: "",
        desc: "sharedurl",
      },
    })
    const res = await this.app.model.Share.findOne({
      where: {
        file_id: this.ctx.request.body.file_id,
        sharedurl: this.ctx.request.body.sharedurl.split("/share/")[1]
      }
    })
    console.log(res, "ress")
    const rows = await this.app.model.File.findOne({
      where: {
        id: res.file_id
      }
    })
    console.log(rows, "ropws")
    this.ctx.apiSuccess(rows, "获取成功", 200)
  }
}

module.exports = HomeController;
